class Tetrada:

    operationList = {
# arith
        '+'  : (2, 1),
        '-'  : (2, 1),
        '*'  : (2, 1),
        '/'  : (2, 1),
        ':=' : (1, 1),

# conver
        'A2I' : (1, 1),
        'I2A' : (1, 1),
        'SUBS': (2, 1),

# labels
        'DEFL' : (1, 0), # define arg1
        'BRL'  : (1, 0), # goto arg1
        'BF'   : (2, 0), # if (arg2) goto arg1
        'BZL'  : (2, 0), # if (arg2 == 0) goto arg1
        'BPL'  : (2, 0), # if (arg2 > 0) goto arg1
        'BML'  : (2, 0), # if (arg2 < 0) goto arg1

# function
        'CALL' : (1, 0),
        'RET'  : (0, 0),

# IO
        'PRINT' : (1, 0),
        'READ'  : (0, 1),

# helpers. just skip them
        'VARDECL' : (1, 0),
        'RETVAL'  : (1, 0),
        'ERROR'   : (0, 0),
    }

    def __init__(self, operation, arg1, arg2, result):
        assert(operation in Tetrada.operationList.keys())
        inn, outn = Tetrada.operationList[operation]
        assert((inn  >= 2) == bool(arg2))
        assert((inn  >= 1) == bool(arg1))
        assert((outn >= 1) == bool(result))

        self.operation = operation
        self.arg1      = arg1
        self.arg2      = arg2
        self.result    = result
        self.line      = None

    def setLine(self, line):
        self.line = line

    def __str__(self):
        res = ''
        if self.result:
            res += str(self.result) + ' <- '
        res += str(self.operation)
        if self.arg1:
            res += '('
            res += str(self.arg1)
            if self.arg2:
                res += ', '
                res += str(self.arg2)
            res += ')'

        fres = ''
        if self.result:
            fres += self.result.getView() + ' <- '
        fres += str(self.operation)
        if self.arg1:
            fres += '('
            fres += self.arg1.getView()
            if self.arg2:
                fres += ', '
                fres += self.arg2.getView()
            fres += ')'
        if self.line != None:
            return '%-20s | %-30s | %s' % (res, fres, self.line)
        else:
            return '%-20s | %-30s' % (res, fres)
